package tk.mybatis.springboot.util;

import cn.hutool.json.JSONException;
import cn.hutool.json.JSONObject;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
//import org.json.JSONException;
//import org.json.JSONObject;
import org.apache.commons.io.IOUtils;
import tk.mybatis.springboot.entity.User;
import cn.hutool.json.JSONUtil;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

public class HttpClientPostExample {

    public static void main(String[] args) {
        try {
            performHttpPostAndCloseHttpClient();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (JSONException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
    }

        public static void performHttpPostAndCloseHttpClient() throws IOException, InvocationTargetException, IllegalAccessException, NoSuchMethodException {

            // 创建 CloseableHttpClient
            CloseableHttpClient httpClient = createHttpClient();

            // 创建 HttpPost 请求
            HttpPost httpPost = new HttpPost("http://localhost:9090/login");

            // 设置请求头 Content-Type 为 application/json
            httpPost.setHeader("Content-Type", "application/json");

            // 创建 JSON 字符串并设置为请求实体
            JSONObject jsonObject = new JSONObject();
            User user = new User();
//            user.setUsername("admin");
//            user.setPassword("123456");
//            jsonObject.put("username", "admin");
//            jsonObject.put("password", "123456");

            StringEntity stringEntity = new StringEntity(JSONUtil.toJsonStr(user), ContentType.APPLICATION_JSON);
            httpPost.setEntity(stringEntity);

            // 执行请求
            CloseableHttpResponse httpResponse = httpClient.execute(httpPost);

            // 处理响应
            int statusCode = httpResponse.getStatusLine().getStatusCode();
            HttpEntity entity = httpResponse.getEntity();
            String name = entity.getContentType().getName();
            Map<String, String> map = BeanUtils.describe(entity);
            System.out.println("响应状态码: " + statusCode);
            //String s = IOUtils.toString(entity.getContent());
            String s = EntityUtils.toString(entity);

            // 关闭 HttpClient
            httpClient.close();
        }


    public static CloseableHttpClient createHttpClient() {
        // 创建 HttpClientBuilder
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();

        // 设置配置（可选）

        // 构建 CloseableHttpClient
        return httpClientBuilder.build();
    }

}
